package com.springboot.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.domain.Doc;
import com.springboot.dto.req.DocQueryRequestDTO;
import com.springboot.dto.resp.DocResponseDTO;
import com.springboot.dto.resp.PageResponse;
import com.springboot.dto.resp.Result;
import com.springboot.service.DocService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/doc")
public class DocController {

    @Resource
    private DocService docService;

    // 查询全部doc信息
    @RequestMapping("/selectByPage")
    public Result<PageResponse<DocResponseDTO>> list(@Valid DocQueryRequestDTO docQueryRequestDTO) {
        // 查询条件
        QueryWrapper<Doc> queryWrapper = new QueryWrapper<>();
        // 排序
        queryWrapper.orderByAsc("sort");
        // 获取查询结果
        Page<Doc> page = new Page<>(docQueryRequestDTO.getPage(), docQueryRequestDTO.getSize());
        Page<Doc> categoryPage = docService.page(page, queryWrapper);
        // 封装为DTO
        PageResponse<DocResponseDTO> pageResponse = new PageResponse<>();
        List<DocResponseDTO> docResponseDTOS = BeanUtil.copyToList(categoryPage.getRecords(), DocResponseDTO.class);
        // 封装分页信息
        pageResponse.setTotal(docService.count());
        pageResponse.setList(docResponseDTOS);
        // 返回
        return Result.success(pageResponse);
    }

}
